<?php

Yii::import('campanias.models._base.BaseCampaniaTpHasCo');

class CampaniaTpHasCo extends BaseCampaniaTpHasCo {

    /**
     * @return CampaniaTpHasCo
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function label($n = 1) {
        return Yii::t('app', 'CampaniaTpHasCo|CampaniaTpHasCos', $n);
    }

    /**
     * @author Alex Yepez <ayepez@tradesystem.com.ec>
     * retorna un array con los clientes de la campaña que pertenecen a una tarea programada
     * @param type $tarea_id
     */
    public function getArrayRegistroClientes($tarea_id) {
        $command = Yii::app()->db->createCommand()
                ->select("
                        cthp.campania_clientes_owner_id
                        ")
                ->from('campania_tp_has_co cthp')
                ->where('cthp.campania_tarea_programada_id = :tarea_id', array(':tarea_id' => $tarea_id));
        return $command->queryColumn();
    }

    /**
     * @author Alex Yepez <ayepez@tradesystem.com.ec>
     * retorna el conjunto de contactos segun la tarea y la acción a realizar
     * @param type $tarea_id
     * @param type $medioContacto
     * @return type
     */
    public function getClientesTarea($tarea_id, $medioContacto = null) {
        $command = Yii::app()->db->createCommand()
                ->select("
                        co.id as [id],
                        co.nombre as [nombre],
                        co.celular as [celular],
                        co.apellido as [apellidos],
                        co.titulo as [titulo],
                        co.cargo as [cargo],
                        co.departamento as [departamento],
                        co.telefono as [telefono],
                        co.fax as [fax],
                        co.email_1 as [email_principal],
                        co.email_2 as [email_secundario]
                        ")
                ->from('campania_tp_has_co cthp')
                ->leftJoin('campania_clientes_owner cco', 'cthp.campania_clientes_owner_id = cco.id')
                ->leftJoin('contacto co', 'cco.cliente_id = co.id')
                ->where('cthp.campania_tarea_programada_id = :tarea_id', array(':tarea_id' => $tarea_id));
        //todos los que tengan celular
        if ($medioContacto == "SMS") {
            $command->andWhere("co.celular is not null AND co.celular !=''");
        }
        //todos los que tengan mails
        if ($medioContacto == "EMAIL") {
            $command->andWhere("(co.email_1 is not null AND co.email_1 !='') OR (co.email_2 is not null AND co.email_2 !='')");
        }
        return $command->queryAll();
    }

}
